package com.sangeng.controller;

import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.AddArticleDto;
import com.sangeng.domain.dto.UpdateArticleDto;
import com.sangeng.domain.vo.ArticleVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/content/article")
@RequiredArgsConstructor
public class ArticleController {
    /* 改为构造器注入 由于加入了@RequiredArgsConstructor注解，不用再写构造方法编写构造器注入
     @Autowired
     private ArticleService articleService;
     */
    private final ArticleService articleService;

    /**
     * 根据title查询文章 为后期模糊查询文章做准备
     * 同时，该接口充当文章管理列表查询功能，只要判断有无title传入即可共用这个接口
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> getArticleListByTitle(Integer pageNum, Integer pageSize, String title, String summary) {
        return articleService.getArticleByTitle(pageNum, pageSize, title, summary);
    }

    @PostMapping
    public ResponseResult<String> add(@RequestBody @Validated AddArticleDto article) {
        return articleService.add(article);
    }

    @DeleteMapping("/{idList}")
    public ResponseResult<String> deleteArticleById(@PathVariable List<Long> idList) {
        return articleService.deleteById(idList);
    }

    //修改文章 先获取文章原有信息 然后再更新 两次请求 两个接口
    @GetMapping("/{id}")
    public ResponseResult<ArticleVo>  getArticle(@PathVariable Long id) {
        return articleService.getArticleById(id);
    }
    @PutMapping
    public ResponseResult<String> updateArticle(@RequestBody UpdateArticleDto articleDto) {
        return articleService.updateArticle(articleDto);
    }
}
